package chartCraft;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;


public class ChartCraft {

	public static void main(String[] args) {
		new Welcomer().start();
	}

}

class Welcomer extends JFrame{
	
	private JFileChooser fileChooser = new JFileChooser();


	public Welcomer () {
		super();
		
		setTitle("Quick Start - " + ChartCraftController.APP_NAME);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		setResizable(false);
		
		setLayout(new GridLayout(3,2));
		
		ImageIcon newIcon = new ImageIcon(getClass().getResource("/images/newDocument.gif"));
		JButton newBtn = new JButton("New", newIcon);
		newBtn.setHorizontalTextPosition(SwingConstants.CENTER);
		newBtn.setVerticalTextPosition(SwingConstants.BOTTOM);
		newBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				startNewDocument();
			}	
		});
		add(newBtn);
		
		ImageIcon openIcon = new ImageIcon(getClass().getResource("/images/openDocument.gif"));
		JButton loadBtn = new JButton("Open a File...", openIcon);
		loadBtn.setHorizontalTextPosition(SwingConstants.CENTER);
		loadBtn.setVerticalTextPosition(SwingConstants.BOTTOM);
		loadBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				loadDocument();
			}
		});	
		add(loadBtn);
		
		ImageIcon pieIcon = new ImageIcon(getClass().getResource("/images/pieDemoIcon.png"));
		JButton pieBtn = new JButton("Pie Chart Template", pieIcon);
		pieBtn.setHorizontalTextPosition(SwingConstants.CENTER);
		pieBtn.setVerticalTextPosition(SwingConstants.BOTTOM);
		pieBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				loadTemplate("/template/pieChart");
			}
		});	
		add(pieBtn);
		
		ImageIcon barIcon = new ImageIcon(getClass().getResource("/images/barChartDemoIcon.png"));
		JButton barBtn = new JButton("Bar Chart Template", barIcon);
		barBtn.setHorizontalTextPosition(SwingConstants.CENTER);
		barBtn.setVerticalTextPosition(SwingConstants.BOTTOM);
		barBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {

				loadTemplate("/template/verticalBarChart");
			}
		});	
		add(barBtn);
		
		ImageIcon stackedIcon = new ImageIcon(getClass().getResource("/images/stackedBarDemoIcon.png"));
		JButton stackedBtn = new JButton("Stacked Bar Chart Template", stackedIcon);
		stackedBtn.setHorizontalTextPosition(SwingConstants.CENTER);
		stackedBtn.setVerticalTextPosition(SwingConstants.BOTTOM);
		stackedBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				loadTemplate("/template/horizontalStackedBarChart");
			}
		});	
		add(stackedBtn);
		
		ImageIcon animationIcon = new ImageIcon(getClass().getResource("/images/animationDemoIcon.png"));
		JButton animationBtn = new JButton("Animation Template", animationIcon);
		animationBtn.setHorizontalTextPosition(SwingConstants.CENTER);
		animationBtn.setVerticalTextPosition(SwingConstants.BOTTOM);
		animationBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				loadTemplate("/template/animationChart");
			}
		});	
		add(animationBtn);
		
	}
	
	

	public void start() {
		pack();
		setVisible(true);
	}
	
	public void startNewDocument() {
		new ChartCraftController();
		release();
	}
	
	public void loadDocument() {
		int returnVal = fileChooser.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fileChooser.getSelectedFile();
			if (ChartCraftController.OpenFromFile(file) != null) {
				release();
			}
		}
	}
	
	public void loadTemplate(String resourceName) {
		if (ChartCraftController.OpenFromTemplate(getClass().getResourceAsStream(resourceName)) != null) {
			release();
		}	
	}
	
	public void release() {
		setVisible(false);
		removeAll();
		fileChooser = null;
	}
}
